﻿CREATE OR REPLACE FUNCTION change_project_currency() RETURNS trigger AS '
                    DECLARE
                      changed boolean;
                    BEGIN
                      changed := new.default_curr is TRUE;
                      IF changed THEN
                         update project_currency set default_curr=False where id in
                                                 (Select id from project_currency where contract_id in
                                                (select distinct contract_id from project_currency where id=new.id)) and id!=new.id;
                        END IF;
                      RETURN new;
                    END
                    ' LANGUAGE plpgsql;
-- CREATE TRIGGER change_default_currency AFTER INSERT OR UPDATE ON project_currency
--                             FOR EACH ROW EXECUTE PROCEDURE change_project_currency();